home *** CD-ROM | disk | FTP | other *** search
/ L' Effet Pommier 3 / L'Effet Pommier - Volume 03.iso / Programmation / Gooey1.3.1 / C Templates / mmMain.c < prev    next >
C/C++ Source or Header  |  1995-01-31  |  29KB  |  869 lines

  1. $$Message Main, mm:mmMain$Prototype.name$.c
  2. $$File mm:mmMain$Prototype.name$.c
  3. /* mmMain$Prototype.name$ */
  4.  
  5. /*    
  6.     Program name:  mmMain$Prototype.name$  
  7.     Function:  This is the main module for this program.  
  8.     History: $Date$ Original by $Author$
  9.  
  10.    */
  11.  
  12. #include "mmCommon$Prototype.name$.h"            /* Common */
  13. #include "Common$Prototype.name$.h"            /* Common */
  14.  
  15. #include "InitExit$Prototype.name$.h"            /* Init, Exit handlers */
  16. #include "events$Prototype.name$.h"                /* Extra event handlers */
  17.  
  18. $$Loop Windows
  19. #include "$Worksheet.name$.h"                    /* Window, $Worksheet.FullName$ */
  20. $$EndLoop
  21. #include "mmMenu$Prototype.name$.h"                /* Menus */
  22.  
  23.  
  24. Boolean        DoIt;                                /* Flag saying an event is ready */
  25. short        code;                                /* Determine event type */
  26. WindowPtr    whichWindow;                        /* See which window for event */
  27. long        mResult;                            /* Menu list and item selected values */
  28. short        theMenu,theItem;                    /* Menu list and item selected */
  29. Boolean        Is_A_Dialog;                        /* Flag for modless dialogs */
  30. short        charCode, itemHit;                    /* For modeless dialogs*/
  31. char        ch;                                    /* Key pressed in Ascii */
  32. Boolean        DoTheModelessEvent, CmdDown;        /* For modeless dialogs*/
  33. WindowPeek    thePeeked;                            /* For modeless dialogs*/
  34.  
  35. /* Handle key strokes */
  36. static void DoKeyEvent(void);
  37.  
  38. /* Handle a diskette inserted */
  39. static void DoDiskEvent(void);
  40.  
  41. $$if HasZoomWindow
  42. /* Handle a window zoom */
  43. static void DoZoom(WindowPtr whichWindow, short code);
  44.  
  45. $$endif
  46. $$if HasResizableWindows
  47. /* Handle a window being resized */
  48. static void DoGrow(WindowPtr whichWindow);
  49.  
  50. $$endif
  51. /* Handle a window being dragged */
  52. static void DoDrag(WindowPtr whichWindow);
  53.  
  54. $$if HasGoAwayBoxes
  55. /* Handle a window goaway box */
  56. static void DoGoAway(WindowPtr whichWindow);
  57.  
  58. $$endif
  59. /* Handle an update to the window */
  60. static void DoUpdate(void);
  61.  
  62. /* Handle the HighLevel event */
  63. static void DoHighLevelEvent(void);
  64.  
  65. /* MAIN entry point */
  66. void main(void);
  67.  
  68.  
  69. /* ======================================================= */
  70.  
  71. /* Routine: WNEIsImplemented */
  72. /* Purpose: See if the MultiFinder trap, WaitNextEvent, is available */
  73.  
  74. Boolean IsWNEIsImplemented(void)                    /* See if WaitNextEvent is available */
  75. {
  76. #define    WNETrapNumber    0xA860                    /* The expected trap number */
  77. #define    kGestaltTrapID    0xA1AD                    /* The expected trap number */
  78.  
  79. SysEnvRec    theWorld;                            /* Environment record */
  80. OSErr        discardError;                        /* Error code returned */
  81. Boolean        theWNEIsImplemented;                /* Value to return */
  82. long        result;                                /* Value returned */
  83.  
  84.  
  85. Black_ForeColor.red = 0x0000;  Black_ForeColor.green = 0x0000;  Black_ForeColor.blue = 0x0000;  /* Get black color */
  86. White_BackColor.red = 0xFFFF;  White_BackColor.green = 0xFFFF;  White_BackColor.blue = 0xFFFF;  /* Get white color */
  87.  
  88. Has.ColorQD = FALSE;                                /* Init to no color QuickDraw */
  89. Has.FPU = FALSE;                                    /* Init to no floating point chip */
  90. Has.AppleEvents = FALSE;                            /* Whether AppleEvents are available */
  91. Has.AliasMgr = FALSE;                                /* Whether AliasMgr is available */
  92. Has.EditionMgr = FALSE;                                /* Whether EditionMgr is available */
  93. Has.Gestalt = FALSE;                                /* Whether Gestalt is available */
  94. Has.NewStdFile = FALSE;                                /* Whether NewStdFile is available */
  95. Has.PPCToolbox = FALSE;                                /* Whether PPCToolbox is available */
  96. Has.QuickDraw32Bit = FALSE;                            /* Whether 32Bit QuickDraw is available */
  97. InTheForeground = TRUE;                                /* Init to a foreground app */
  98.  
  99. discardError = SysEnvirons(1, &theWorld);            /* Check how old this system is */
  100. if (theWorld.machineType < 0)                        /* Negative means really old */
  101.     {
  102.     theWNEIsImplemented = FALSE;                    /* Really old ROMs, no WNE possible */
  103.     }
  104. else
  105.     {
  106.     theWNEIsImplemented = true;
  107.     Has.ColorQD = theWorld.hasColorQD;                /* Flag for Color QuickDraw being available */
  108.     Has.FPU = theWorld.hasFPU;                        /* Flag for Floating Point Math Chip being available */
  109.     Has.Gestalt = true;
  110.     if (Has.Gestalt)                                /* Do if Gestalt is available */
  111.         {
  112.         discardError = Gestalt(gestaltAliasMgrAttr,&result);
  113.         if ((discardError == 0) && ((result & (0x00000001 << gestaltAliasMgrPresent)) != 0))
  114.             Has.AliasMgr = TRUE;
  115.  
  116.         discardError = Gestalt(gestaltEditionMgrAttr,&result);
  117.         if ((discardError == 0) && ((result & (0x00000001 << gestaltEditionMgrPresent)) != 0))
  118.             Has.EditionMgr = TRUE;
  119.  
  120.         discardError = Gestalt(gestaltAppleEventsAttr,&result);
  121.         if ((discardError == 0) && ((result & (0x00000001 << gestaltAppleEventsPresent)) != 0))
  122.             Has.AppleEvents = TRUE;
  123.  
  124.         discardError = Gestalt(gestaltPPCToolboxAttr,&result);
  125.         if ((discardError == 0) && ((result & (0x00000001 << gestaltPPCToolboxPresent)) != 0))
  126.             Has.PPCToolbox = TRUE;
  127.  
  128.         discardError = Gestalt(gestaltQuickdrawVersion,&result);
  129.         if ((discardError == 0) && ((result & gestalt32BitQD) != 0))
  130.             Has.QuickDraw32Bit = TRUE;
  131.  
  132.         discardError = Gestalt(gestaltStandardFileAttr,&result);
  133.         if ((discardError == 0) && ((result & (0x00000001 << gestaltStandardFile58)) != 0))
  134.             Has.NewStdFile = TRUE;
  135.         }
  136.     }
  137.     
  138. return(theWNEIsImplemented);
  139. }
  140.  
  141. /* ======================================================= */
  142.  
  143. /* Routine: Handle_User_Event */
  144. /* Purpose: Check for user events */
  145.  
  146. void Handle_User_Event(void)                        /* Check for user events */
  147. {
  148. UserEventRec    TheUserEvent;                    /* The user event */
  149.  
  150.  
  151. GetUserEvent(&TheUserEvent);                    /* Check for any user events */
  152. if (TheUserEvent.ID != UserEvent_None)            /* Only do if we have any */
  153.     {
  154.  
  155.     switch (TheUserEvent.ID)                    /* Key off the Event ID */
  156.         {
  157.         case UserEvent_Open_Window:             /* Open a Window or Modeless dialog */
  158.             switch (TheUserEvent.ID2)            /* Do the appropiate window */
  159.                 {
  160.                 $$Loop Alerts
  161.                 case ResA_$Worksheet.name$:
  162.                     PA_$Worksheet.name$();            /* Open this alert */
  163.                     break;
  164.                 $$EndLoop
  165.                 $$Loop ModalDialogs
  166.                 case ResD_$Worksheet.name$:
  167.                     MPD_$Worksheet.name$();            /* Open this modal dialog */
  168.                     break;
  169.                 $$EndLoop
  170.                 $$Loop ModelessDialogs
  171.                 case ResD_$Worksheet.name$:
  172.                     Open_$Worksheet.name$();        /* Open this modeless dialog */
  173.                     break;
  174.                 $$EndLoop
  175.                 $$Loop Windows
  176.                 case ResW_$Worksheet.name$: 
  177.                     Open_$Worksheet.name$();            /* Open this window */
  178.                     break;
  179.                 $$EndLoop
  180.                 default:                        /* Handle others */
  181.                     break;
  182.                 }                                /* End of the switch */
  183.             break;
  184.  
  185.         case UserEvent_Close_Window:             /* Close a Window or Modeless dialog */
  186.             switch (TheUserEvent.ID2) {            /* Do the appropiate window */
  187.                 $$Loop ModelessDialogs
  188.                 case ResD_$Worksheet.name$:
  189.                     Close_$Worksheet.name$(Rec_$Worksheet.name$.theDialog);/* Close this modeless dialog */
  190.                     break;
  191.                 $$EndLoop
  192.                 $$Loop Windows
  193.                 case ResW_$Worksheet.name$: 
  194.                     Close_$Worksheet.name$((WindowPtr)-1);        /* Close this window */
  195.                     break;
  196.                 $$EndLoop
  197.                 default:                        /* Handle others */
  198.                     break;
  199.                 }                                /* End of the switch */
  200.             break;
  201.  
  202.         default:                                /* Not standard, must be program specific */
  203.             Handle_UserEvent(&TheUserEvent);    /* Let program specific handle it */
  204.             break;
  205.         }                                        /* End of switch */
  206.     }
  207. }
  208.  
  209. /* ======================================================= */
  210.  
  211. /* Routine: DoKeyEvent */
  212. /* Purpose: Handle a key pressed */
  213.  
  214. static void DoKeyEvent(void)                            /* Handle key presses */
  215. {
  216. short    charCode;                                    /* Key code */
  217. char    ch;                                            /* Key pressed in Ascii */
  218. long    mResult;                                    /* Menu list and item, if a command key */
  219. short    theMenu,theItem;                            /* Menu list and item, if command key */
  220.  
  221.  
  222. if (HandleKey(&myEvent))                             /* Allow for special key handling */
  223.     {
  224.     charCode = myEvent.message & charCodeMask;        /* Get the character */
  225.     ch = (char)charCode;                            /* Change it to ASCII */
  226.  
  227.     if ((myEvent.modifiers & cmdKey) != 0)            /* See if Command key is down */
  228.         {
  229.         U_EnableMenus();                            /* Let us in to enable and disable menus*/
  230.         mResult = MenuKey(ch);                        /* See if a menu selection */
  231.         theMenu = HiWord(mResult);                    /* Get the menu list number */
  232.         theItem = LoWord(mResult);                    /* Get the menu item number */
  233.         if (theMenu != 0)                            /* See if a list was selected */
  234.             Handle_My_Menu(theMenu, theItem);        /* Do the menu selection */
  235.  
  236.         if (((ch == 'x') || (ch == 'X')) && (theInput != NIL))
  237.             TECut(theInput);                        /* Handle a Cut in a TE area */
  238.         if (((ch == 'c') || (ch == 'C')) && (theInput != NIL))
  239.             TECopy(theInput);                        /* Handle a Copy in a TE area */
  240.         if (((ch == 'v')  ||  (ch == 'V')) && (theInput != NIL))
  241.             TEPaste(theInput);                        /* Handle a Paste in a TE area */
  242.         }
  243.     else if (theInput != NIL)
  244.         TEKey(ch,theInput);                            /* Place the normal key stroke */
  245.     }
  246. }
  247.  
  248. /* ======================================================= */
  249.  
  250. /* Routine: DoDiskEvent */
  251. /* Purpose: Handle a diskette inserted */
  252.  
  253. static void DoDiskEvent(void)                            /* Handle disk inserted */
  254. {
  255. short    theError;                                    /* Error returned from mount */
  256.  
  257.  
  258. if (HandleDisk(&myEvent))                            /* Allow for special disk inserted handling */
  259.     {
  260.     if (HiWord(myEvent.message) != noErr)            /* See if a diskette mount error */
  261.         {
  262.         myEvent.where.h = ((screenRect.bounds.right - screenRect.bounds.left) / 2) - (304 / 2);/* Center horz */
  263.         myEvent.where.v = ((screenRect.bounds.bottom - screenRect.bounds.top) / 3) - (104 / 2);/* Top 3ed vertically */
  264.         InitCursor();                                /* Make sure it has an arrow cursor */
  265.         theError = DIBadMount(myEvent.where, myEvent.message);/* Let the OS handle the diskette */
  266.         }
  267.     }
  268. }
  269.  
  270. $$if HasZoomWindow
  271. /* ======================================================= */
  272.  
  273. /* Routine: DoZoom */
  274. /* Purpose: Handle a window zoom */
  275.  
  276. static void DoZoom(WindowPtr whichWindow, short code)/* Handle a window zoom */
  277. {
  278. Rect    OldRect;                                    /* Window rect before the zoom */
  279. Point    myPt;                                        /* Point for tracking zoom box */
  280. long    theRefCon;                                    /* Refcon with layer masked off */
  281.  
  282.  
  283. if (!Doing_MovableModal)                            /* Select proper window */
  284.     {
  285.     if (whichWindow != NIL)                            /* See if we have a legal window */
  286.         {
  287.         SetPort(whichWindow);                        /* Get ready to draw in this window */
  288.  
  289.         myPt = myEvent.where;                        /* Get mouse position */
  290.         GlobalToLocal(&myPt);                        /* Make it relative */
  291.  
  292.         OldRect = whichWindow->portRect;            /* Save the rect before resizing */
  293.  
  294.         if (TrackBox(whichWindow, myPt, code))        /* Zoom it */
  295.             {
  296.             ZoomWindow(whichWindow, code, TRUE);    /* Resize to result */
  297.             EraseRect(&whichWindow->portRect);        /* Make sure we update the whole window effectively */
  298.             InvalRect(&whichWindow->portRect);        /* Tell ourselves to update, redraw, the window contents */
  299.             theRefCon = GetWRefCon(whichWindow);    /* Get the refcon */
  300.             theRefCon = theRefCon & 0x00FFFFFF;        /* Mask the layer out */
  301.  
  302.             switch (theRefCon)                        /* Do the appropiate window */
  303.                 {
  304.                 $$Loop Windows
  305.                 $$if Window.Zoomable
  306.                 case ResW_$Worksheet.name$:
  307.                     Resized_$Worksheet.name$(&OldRect, whichWindow);/* Resized this window */
  308.                     break;
  309.                 $$endif
  310.                 $$EndLoop
  311.  
  312.                 default:                            /* Handle others */
  313.                     U_DoZoom(&OldRect, whichWindow);/* Allow user to handle others */
  314.                     break;
  315.                 }
  316.             Mk_HiliteWindow(whichWindow);            /* Hilite it again */
  317.             }
  318.         }
  319.     }
  320. }
  321.  
  322. $$endif
  323. $$if HasResizableWindows
  324. /* ======================================================= */
  325.  
  326. /* Routine: DoGrow */
  327. /* Purpose: Handle a window resize */
  328.  
  329. static void DoGrow(WindowPtr whichWindow)
  330. {
  331. Rect    OldRect;                                    /* Window rect before the grow */
  332. Point    myPt;                                        /* Point for tracking grow box */
  333. Rect    GrowRect;                                    /* Set the grow bounds */
  334. long    mResult;                                    /* Result from the grow */
  335. long    theRefCon;                                    /* Refcon with layer masked off */
  336.  
  337.  
  338. if (!Doing_MovableModal)                            /* Select proper window */
  339.     {
  340.     if (whichWindow != nil)                            /* See if we have a legal window */
  341.         {
  342.         SetPort(whichWindow);                        /* Get ready to draw in this window */
  343.  
  344.         myPt = myEvent.where;                        /* Get mouse position */
  345.         GlobalToLocal(&myPt);                        /* Make it relative */
  346.  
  347.         OldRect = whichWindow->portRect;            /* Save the rect before resizing */
  348.  
  349.         SetRect(&GrowRect, 4, 4, (screenRect.bounds.right - screenRect.bounds.left)-4,
  350.             (screenRect.bounds.bottom - screenRect.bounds.top) - 4);/* l,t,r,b */
  351.         mResult = GrowWindow(whichWindow, myEvent.where, &GrowRect);/* Grow it */
  352.         SizeWindow(whichWindow, LoWord(mResult), HiWord(mResult), true);/* Resize to result */
  353.  
  354.         theRefCon = GetWRefCon(whichWindow);        /* Get the refcon */
  355.         theRefCon = theRefCon & 0x00FFFFFF;            /* Mask the layer out */
  356.  
  357.         switch (theRefCon)                            /* Do the appropiate window */
  358.             {
  359.             $$Loop Windows
  360.             $$if Window.Resizable
  361.             case ResW_$Worksheet.name$:
  362.                 Resized_$Worksheet.name$(&OldRect, whichWindow);/* Resized this window */
  363.                 break;
  364.             $$endif
  365.             $$EndLoop
  366.  
  367.             default:                                /* allow other buttons, trap for debug */
  368.                 U_DoGrow(&OldRect, whichWindow);    /* Allow user to handle others */
  369.                 break;                                /* end of otherwise */
  370.             }                                        /* end of switch */
  371.  
  372.         SetPort(whichWindow);                        /* Get ready to draw in this window */
  373.  
  374.         myPt.h = whichWindow->portRect.right - whichWindow->portRect.left;/* Local right edge */
  375.         myPt.v = whichWindow->portRect.bottom - whichWindow->portRect.top;/* Local bottom edge */
  376.  
  377.         SetRect(&GrowRect, 0, OldRect.bottom - 15, OldRect.right + 15, OldRect.bottom + 15);/* Position for horz scrollbar area */
  378.         EraseRect(&GrowRect);                        /* Erase old area */
  379.         InvalRect(&GrowRect);                        /* Flag us to update it */
  380.  
  381.         SetRect(&GrowRect, OldRect.right - 15, 0, OldRect.right + 15, OldRect.bottom + 15);/* Position for vert scrollbar area */
  382.         EraseRect(&GrowRect);                        /* Erase old area */
  383.         InvalRect(&GrowRect);                        /* Flag us to update it */
  384.  
  385.         DrawGrowIcon(whichWindow);                    /* Draw the grow Icon again */
  386.         }
  387.     }
  388. }
  389.  
  390. $$endif
  391. /* ======================================================= */
  392.  
  393. /* Routine: DoDrag */
  394. /* Purpose: Drag a window around */
  395.  
  396. static void DoDrag(WindowPtr whichWindow)
  397. {
  398. Rect    OldRect;                                /* Window rect before the drag */
  399. Rect    tempRect;                                /* temporary rect */
  400. long    theRefCon;                                /* Refcon with layer masked off */
  401.  
  402.  
  403. if ((!Doing_MovableModal) || (Doing_MovableModal && (whichWindow == FrontWindow())))/* See if OK to drag */
  404.     {
  405.     OldRect = whichWindow->portRect;            /* Save the rect before resizing */
  406.  
  407.     if (!Doing_MovableModal)                    /* Select proper window */
  408.         Mk_HiliteWindow(whichWindow);
  409.     SetRect(&tempRect, -32000, -32000, 32000, 32000);
  410.     Mk_DragWindow(whichWindow, myEvent.where, tempRect);/* Drag the window */
  411.  
  412.     theRefCon = GetWRefCon(whichWindow);        /* Get the refcon */
  413.     theRefCon = theRefCon & 0x00FFFFFF;            /* Mask the layer out */
  414.     switch (theRefCon)                            /* Do the appropiate window */
  415.         {
  416.         $$Loop ModelessDialogs
  417.         case ResD_$Worksheet.name$:
  418.             Moved_$Worksheet.name$(&OldRect, whichWindow);    /* Moved this modeless dialog */
  419.             break;
  420.         $$EndLoop
  421.         $$Loop Windows
  422.         case ResW_$Worksheet.name$:
  423.             U_Moved$Worksheet.name$(&OldRect, whichWindow);/* Moved this window */
  424.             break;
  425.         $$EndLoop
  426.  
  427.         default:                                /* allow other buttons, trap for debug */
  428.             U_Moved(&OldRect, whichWindow);        /* Allow user to handle others */
  429.             break;                                /* end of otherwise */
  430.         }                                        /* end of switch */
  431.     }
  432. }
  433.  
  434. $$if HasGoAwayBoxes
  435. /* ======================================================= */
  436.  
  437. /* Routine: DoGoAway */
  438. /* Purpose: Close a window */
  439.  
  440. static void DoGoAway(WindowPtr whichWindow)
  441. {
  442. long    theRefCon;                                /* Refcon with layer masked off */
  443.  
  444.  
  445. if (!Doing_MovableModal)                        /* Select proper window */
  446.     {
  447.     if (TrackGoAway(whichWindow,myEvent.where))    /* See if mouse released in GoAway box */
  448.         {
  449.         theRefCon = GetWRefCon(whichWindow);    /* Get the refcon */
  450.         theRefCon = theRefCon & 0x00FFFFFF;        /* Mask the layer out */
  451.         switch (theRefCon)                        /* Do the appropiate window */
  452.             {
  453.             $$Loop ModelessDialogs
  454.             $$if ModelessDialog.GoAway
  455.             case ResD_$Worksheet.name$:
  456.                 Close_$Worksheet.name$(whichWindow);    /* Close this modeless dialog */
  457.                 break;
  458.             $$endif
  459.             $$EndLoop
  460.             $$Loop Windows
  461.             $$if Window.GoAway
  462.             case ResW_$Worksheet.name$:
  463.                 Close_$Worksheet.name$(whichWindow);    /* Close this window */
  464.                 break;
  465.             $$endif
  466.             $$EndLoop
  467.  
  468.             default:                            /* allow other buttons, trap for debug */
  469.                 U_GoAway(whichWindow);            /* Allow user to handle others */
  470.                 break;                            /* end of otherwise */
  471.             }                                    /* end of switch */
  472.         }
  473.     }
  474. }
  475.  
  476. $$endif
  477. /* ======================================================= */
  478.  
  479. /* Routine: DoInContent */
  480. /* Purpose: Pressed in the content area */
  481.  
  482. void DoInContent(WindowPtr whichWindow,EventRecord *myEvent)
  483. {
  484. long    theRefCon;                                /* Refcon with layer masked off */
  485.  
  486.  
  487. if (!Doing_MovableModal)                        /* Select proper window */
  488.     {
  489.     if (Mk_Is_FrontWindow(whichWindow) == false)    /* See if already selected or not, in front if selected */
  490.         Mk_HiliteWindow(whichWindow);
  491.     else
  492.         {
  493.         SetPort(whichWindow);                    /* Get ready to draw in this window */
  494.         Mk_HiliteWindow(whichWindow);
  495.         theRefCon = GetWRefCon(whichWindow);    /* Get the refcon */
  496.         theRefCon = theRefCon & 0x00FFFFFF;        /* Mask the layer out */
  497.  
  498.         switch (theRefCon)                        /* Do the appropiate window */
  499.             {
  500.             $$Loop Windows
  501.             case ResW_$Worksheet.name$:
  502.                 Do_$Worksheet.name$(myEvent);        /* Handle this window */
  503.                 break;
  504.             $$EndLoop
  505.  
  506.             default:                            /* allow other buttons, trap for debug */
  507.                 U_InContent(myEvent,whichWindow);    /* Allow user to handle others */
  508.                 break;                            /* end of otherwise */
  509.             }                                    /* end of switch */
  510.         }
  511.     }
  512. }
  513.  
  514. /* ======================================================= */
  515.  
  516. /* Routine: DoUpdate */
  517. /* Purpose: Got an update event */
  518.  
  519. static void DoUpdate(void)
  520. {
  521. WindowPtr    whichWindow;                        /* See which window for event */
  522. long        theRefCon;                            /* Refcon with layer masked off */
  523.  
  524.  
  525. whichWindow = (WindowPtr)myEvent.message;        /* Get the window the update is for */
  526.  
  527. BeginUpdate(whichWindow);                        /* Set the clipping to the update area */
  528. theRefCon = GetWRefCon(whichWindow);            /* Get the refcon */
  529. theRefCon = theRefCon & 0x00FFFFFF;                /* Mask the layer out */
  530. switch (theRefCon)                                /* Do the appropiate window */
  531.     {
  532.     $$Loop Windows
  533.     case ResW_$Worksheet.name$:
  534.         Update_$Worksheet.name$(whichWindow);        /* Update this window */
  535.         break;
  536.     $$EndLoop
  537.  
  538.     default:                                    /* allow other buttons, trap for debug */
  539.         U_Update(whichWindow);                    /* Allow user to handle others */
  540.         break;                                    /* end of otherwise */
  541.     }                                            /* end of switch */
  542. EndUpdate(whichWindow);                            /* Return to normal clipping area */
  543. }
  544.  
  545. /* ======================================================= */
  546.  
  547. /* Routine: DoActivate */
  548. /* Purpose: Got an activate or deactivate event */
  549.  
  550. void DoActivate(void)
  551. {
  552. WindowPtr    whichWindow;                        /* See which window for event */
  553. Boolean        Do_An_Activate;                        /* Flag to pass */
  554. long        theRefCon;                            /* Refcon with layer masked off */
  555.  
  556.  
  557. whichWindow = (WindowPtr)myEvent.message;        /* Get the window the update is for */
  558.  
  559. Do_An_Activate =  ((myEvent.modifiers & 0x0001) != 0);/* Make sure it is Activate and not DeActivate */
  560. theRefCon = GetWRefCon(whichWindow);            /* Get the refcon */
  561. theRefCon = theRefCon & 0x00FFFFFF;                /* Mask the layer out */
  562. switch (theRefCon)                                /* Do the appropiate window */
  563.     {
  564.     $$Loop Windows
  565.     case ResW_$Worksheet.name$:
  566.         Activate_$Worksheet.name$(whichWindow,Do_An_Activate);    /* Activate or deactivate this window */
  567.         break;
  568.     $$EndLoop
  569.  
  570.     default:                                    /* allow other buttons, trap for debug */
  571.         U_Activate(whichWindow,Do_An_Activate);    /* Allow user to handle others */
  572.         break;                                    /* end of otherwise */
  573.     }                                            /* end of switch */
  574. }
  575.  
  576. /* ======================================================= */
  577.  
  578. /* Routine: DoHighLevelEvent */
  579. /* Purpose: Got a High Level event */
  580.  
  581. static void DoHighLevelEvent(void)
  582. {
  583. OSErr    err;                                    /* Error */
  584.  
  585.  
  586. err = AEProcessAppleEvent(&myEvent);            /* Go to the processor of apple events */
  587. }
  588.  
  589. /* ======================================================= */
  590. /* ======================================================= */
  591.  
  592.  void main(void)                                    /* Start of main body */
  593. {
  594.  
  595. MoreMasters();                                    /* This reserves space for more handles */
  596. MaxApplZone();                                    /* Give us room for memory allocation */
  597. InitGraf(&QDPort);                            /* Quickdraw Init */
  598. InitFonts();                                    /* Font manager init */
  599. InitWindows();                                    /* Window manager init */
  600. InitMenus();                                    /* Menu manager init */
  601. TEInit();                                        /* Text edit init */
  602. InitDialogs(0L);                                /* Dialog manager */
  603.  
  604. FlushEvents(everyEvent , 0);                    /* Clear out all events */
  605. InitCursor();                                    /* Make an arrow cursor */
  606.  
  607. doneFlag = FALSE;                                /* Do not exit program yet */
  608.  
  609. OpenSplashWindow();
  610.  
  611. Init_My_Menus();                                /* Initialize menu bar */
  612.  
  613. theInput = nil;                                    /* Init to no text edit selection active */
  614.  
  615. SleepValue = 40;                                /* Set sleep value */
  616. WNE = IsWNEIsImplemented();                        /* See if WaitNextEvent is available */
  617.  
  618. UserEventList = nil;                            /* No user events yet */
  619.  
  620. cursorRgn = NewRgn();                            /* Cursor region for WaitNextEvent */
  621.  
  622. if (Has.AppleEvents)                            /* See if AppleEvents are available */
  623.     InitAppleEvents();                            /* Initialize for AppleEvents */
  624.  
  625. Printing.hPrint = nil;                            /* Init to no print record yet */
  626. Printing.PrinterIsOpen = false;                    /* We are not printing yet */
  627.  
  628. U_InitPreferences();                            /* Set default preferences */
  629.  
  630. $$Loop Alerts
  631. InitA_$Worksheet.name$();                            /* Initialize Alert, $Worksheet.FullName$ */
  632. $$EndLoop
  633. $$Loop ModalDialogs
  634. InitD_$Worksheet.name$();                            /* Initialize Modal Dialog, $Worksheet.FullName$ */
  635. $$EndLoop
  636. $$Loop ModelessDialogs
  637. Init_$Worksheet.name$();                            /* Initialize Modeless Dialog, $Worksheet.FullName$ */
  638. $$EndLoop
  639. $$Loop Windows
  640. Init_$Worksheet.name$();                            /* Initialize Window, $Worksheet.FullName$ */
  641. $$EndLoop
  642.  
  643. Mk_ClearLayers();                                /* Init layer array */
  644. Doing_MovableModal = false;                        /* Not currently doing a movable modal */
  645.  
  646. ApplInit$Prototype.name$();                        /* Handle extra program initialization */
  647.  
  648. GetPreferences();                                /* Get preferences */
  649.  
  650. $$Loop Alerts
  651. $$if Worksheet.OpenAtStartup
  652. PA_$Worksheet.name$();                            /* Open at program start, $Worksheet.FullName$ */
  653. $$endif
  654. $$EndLoop
  655. $$Loop ModalDialogs
  656. $$if Worksheet.OpenAtStartup
  657. MPD_$Worksheet.name$();                            /* Open at program start, $Worksheet.FullName$ */
  658. $$endif
  659. $$EndLoop
  660. $$Loop ModelessDialogs
  661. $$if Worksheet.OpenAtStartup
  662. Open_$Worksheet.name$();                            /* Open at program start, $Worksheet.FullName$ */
  663. $$endif
  664. $$EndLoop
  665. $$Loop Windows
  666. $$if Worksheet.OpenAtStartup
  667. Open_$Worksheet.name$();                            /* Open at program start, $Worksheet.FullName$ */
  668. $$endif
  669. $$EndLoop
  670.  
  671. CloseSplashWindow();
  672.  
  673. do
  674.     {
  675.     ApplLoop$Prototype.name$();                            /* Hook into the main loop */
  676.  
  677.     Handle_User_Event();                            /* Check for user events */
  678.  
  679.     if (theInput != NIL)                            /* See if a TE is active */
  680.         TEIdle(theInput);                            /* Blink the cursor if everything is ok */
  681.  
  682.     if (WNE)                                        /* See if do the MultiFinder way */
  683.         DoIt = WaitNextEvent(everyEvent, &myEvent, SleepValue, cursorRgn);/* Wait for an event */
  684.     else
  685.         {
  686.         SystemTask();                                /* For support of desk accessories */
  687.         DoIt = GetNextEvent(everyEvent, &myEvent);    /* See if an event is ready */
  688.         }
  689.  
  690.     ApplEvent$Prototype.name$(&DoIt,&myEvent);                /* Let us at the event first */
  691.  
  692.     if (DoIt)                                        /* If event then... */
  693.         {
  694.         Is_A_Dialog = IsDialogEvent(&myEvent);        /* See if a modeless dialog event */
  695.         if (Is_A_Dialog == TRUE)                    /* Handle a dialog event */
  696.             {
  697.              if (myEvent.what == updateEvt)            /* Handle the update of a Modeless Dialog */
  698.                 {
  699.                 whichWindow = (WindowPtr)myEvent.message; /* Get the window the update is for */
  700.                 BeginUpdate(whichWindow);            /* Set update clipping area */
  701.                 $$Loop ModelessDialogs
  702.                 Update_$Worksheet.name$(whichWindow);    /* Update Modeless Dialog, $Worksheet.FullName$ */
  703.                 $$EndLoop
  704.                 EndUpdate(whichWindow);                /* Return to normal clipping area */
  705.                 }
  706.             else
  707.                 {
  708.                 DoTheModelessEvent = TRUE;            /* Go ahead and do it so far */
  709.  
  710.                 if (myEvent.what == keyDown)         /* Check the key down, for a command key event */
  711.                     {
  712.                     CmdDown = ((myEvent.modifiers / cmdKey) & 1);/* Get the command key state */
  713.                     charCode = myEvent.message & charCodeMask;/* Get the character */
  714.                     ch = (char)charCode;            /* Change it to ASCII */
  715.  
  716.                     if ((charCode == 13) || (charCode == 0x03))/* CR or Enter */
  717.                         DoTheModelessEvent = TRUE;    /* Handle the default selection */
  718.  
  719.                     if (CmdDown != 0)                /* Handle if the command key was down */
  720.                         {
  721.                         U_EnableMenus();            /* Let us in to enable and disable menus*/
  722.                         mResult = MenuKey(ch);        /* See if a menu selection */
  723.                         theMenu = HiWord(mResult);    /* Get the menu list number */
  724.                         theItem = LoWord(mResult);    /* Get the menu item number */
  725.                         if (theMenu != 0)              /* See if a list was selected */
  726.                             Handle_My_Menu(theMenu, theItem); /* Do the menu selection */
  727.  
  728.                         whichWindow = FrontWindow(); /* Get the front window */
  729.                         if ((ch == 'x') || (ch == 'X'))    /* Handle a CUT */
  730.                             DlgCut(whichWindow);
  731.                         if ((ch == 'c') || (ch == 'C'))    /* Handle a COPY */
  732.                             DlgCopy(whichWindow);
  733.                         if ((ch == 'v') || (ch == 'V'))    /* Handle a PASTE */
  734.                             DlgPaste(whichWindow);
  735.  
  736.                         DoTheModelessEvent = FALSE;/* We handled the command key */
  737.                         }
  738.                     }
  739.  
  740.                 if (DoTheModelessEvent == TRUE)     /* Do we handle it? */
  741.                     {
  742.                     if ((DialogSelect(&myEvent, &whichWindow, &itemHit)) || (myEvent.what == mouseDown) || (myEvent.what == keyDown)) /* Ck if do it */
  743.                         {
  744.                         $$Loop ModelessDialogs
  745.                         Do_$Worksheet.name$(&myEvent,whichWindow,itemHit);    /* Handle the Modeless Dialog, $Worksheet.FullName$ */
  746.                         $$EndLoop
  747.                         }
  748.                     }
  749.                 }
  750.             }
  751.         else
  752.             {
  753.  
  754.             switch (myEvent.what)                    /* Decide type of event */
  755.                 {
  756.                 case mouseDown :                /* Mouse button pressed */
  757.                     code = FindWindow(myEvent.where, &whichWindow);/* Get which window the event happened in */
  758.  
  759.                     switch (code)                /* Decide type of event again */
  760.                         {
  761.                         case inMenuBar :        /* In the menubar */
  762.                             U_EnableMenus();                    /* Let us in to enable and disable menus*/
  763.                             mResult = MenuSelect(myEvent.where);/* Do menu selection */
  764.                             theMenu = HiWord(mResult);            /* Get the menu list number */
  765.                             theItem = LoWord(mResult);            /* Get the menu list item number */
  766.                             Handle_My_Menu( theMenu, theItem);    /* Handle the menu */
  767.                             break;
  768.  
  769.                         case inDrag :        /* In window drag area */
  770.                             DoDrag(whichWindow);/* Go drag the window */
  771.                             break;
  772.  
  773.                         $$if HasResizableWindows
  774.                         case inGrow :        /* In window grow area */
  775.                             DoGrow(whichWindow);/* Handle the growing */
  776.                             break;
  777.  
  778.                         $$endif
  779.                         $$if HasZoomWindow
  780.                         case inZoomIn :        /* In window zoom area */
  781.                         case inZoomOut :    /* In window zoom area */
  782.                             DoZoom(whichWindow,code);/* Go zoom the window */
  783.                             break;
  784.  
  785.                         $$endif
  786.                         $$if HasGoAwayBoxes
  787.                         case inGoAway :        /* In window goaway area */
  788.                             DoGoAway(whichWindow);/* Handle the goaway button */
  789.                             break;
  790.  
  791.                         $$endif
  792.                         case inContent :    /* In window  contents */
  793.                             DoInContent(whichWindow,&myEvent);/* Handle the hit inside a window */
  794.                             break;
  795.  
  796.                         case inSysWindow :    /* See if a DA selection */
  797.                             SystemClick(&myEvent, whichWindow);/* Let other programs in */
  798.                             break;
  799.  
  800.                         default:            /* Handle others */
  801.                             break;            /* End of otherwise */
  802.                         }                    /* End of code case */
  803.                     break;                    /* End of MouseDown */
  804.  
  805.                 case keyDown:                /* Handle key inputs */
  806.                 case autoKey:                /* and auto repeats */
  807.                     DoKeyEvent();            /* Get the key and handle it */
  808.                     break;
  809.  
  810.                 case updateEvt :            /* Update event for a window */
  811.                     DoUpdate();                /* Handle the update */
  812.                     break;
  813.  
  814.                 case diskEvt :                /* Disk inserted event */
  815.                     DoDiskEvent();            /* Handle a disk event */
  816.                     break;
  817.  
  818.                 case activateEvt :            /* Window activated event */
  819.                     DoActivate();            /* Handle the activation */
  820.                     break;
  821.  
  822.                 case osEvt:                    /* OS event */
  823.                     DoOSEvent(&myEvent);    /* Handle the activation */
  824.                     break;
  825.  
  826.                 case kHighLevelEvent:        /* High Level event */
  827.                     if (Has.AppleEvents)    /* See if init for Apple Events */
  828.                         DoHighLevelEvent();    /* Do AppleEvents */
  829.                     break;
  830.  
  831.                 default:                    /* Used for debugging, to see what other events are coming in */
  832.                     break;                    /* End of otherwise */
  833.  
  834.                 }                            /* End of case */
  835.  
  836.             }
  837.         }
  838.     else
  839.         {
  840.         whichWindow = FrontWindow();        /* Get the current front window */
  841.         if (whichWindow != NIL)                /* See if we have a window */
  842.             {
  843.             thePeeked = (WindowPeek)whichWindow;        /* Peek inside, look for dialog */
  844.             if (thePeeked->windowKind == dialogKind)    /* DialogSelect will crash if no dialogs */
  845.                 {
  846.                 if (DialogSelect(&myEvent, &whichWindow, &itemHit))/* Blink cursor in modeless TEs */
  847.                     {
  848.                     }
  849.                 }
  850.             }
  851.         }
  852.     }                                        /* end of while */
  853. while (!doneFlag);                            /* End of the event loop */
  854.  
  855. ApplExit$Prototype.name$();                    /* Handle extra program termination code */
  856.  
  857. $$Loop ModelessDialogs
  858. Close_$Worksheet.name$(Rec_$Worksheet.name$.theDialog);/* Close this modeless dialog */
  859. $$EndLoop
  860. $$Loop Windows
  861. Close_$Worksheet.name$((WindowPtr)-1);        /* Close this window */
  862. $$EndLoop
  863.  
  864. SetPreferences();                        /* Set new preferences */
  865.  
  866. }                                            /* end of main */
  867. $$CloseFile
  868.  
  869.